package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Collect;
import cn.wolfcode.trip.base.domain.News;
import cn.wolfcode.trip.base.domain.NewsContent;
import cn.wolfcode.trip.base.mapper.CollectMapper;
import cn.wolfcode.trip.base.mapper.NewsContentMapper;
import cn.wolfcode.trip.base.mapper.NewsMapper;
import cn.wolfcode.trip.base.query.NewsQueryObject;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.INewsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class NewsServiceImpl implements INewsService {
    @Autowired
    private NewsMapper newsMapper;
    @Autowired
    private NewsContentMapper contentMapper;
    @Autowired
    private CollectMapper collectMapper;

    public PageInfo<News> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<News> list = newsMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    public void saveOrUpdate(News entity) {
        //新增时设置状态为未收藏
        if (entity.getState() != null){
            entity.setState(entity.STATE_TEMP);
        }
        //日报文章内容的主键关联
        NewsContent newsContent = entity.getNewsContent();
        if (entity.getId() == null){
            //新增时设置创建时间
            entity.setCreateTime(new Date());
            newsMapper.insert(entity);
            newsContent.setId(entity.getId());
            contentMapper.insert(newsContent);
        } else {
            newsMapper.updateByPrimaryKey(entity);
            newsContent.setId(entity.getId());
            contentMapper.updateByPrimaryKey(newsContent);
        }
    }

    public List<News> list() {
        return null;
    }

    public News get(Long id) {
        News news = newsMapper.selectByPrimaryKey(id);
        NewsContent content = contentMapper.selectByPrimaryKey(id);
        news.setNewsContent(content);
        return news;
    }

    public void changeStateBtn(Long id, Integer isRelease) {
        if (isRelease == News.ISRELEASE_RELEASE){
            //发布操作
            //设置发布的时间
            newsMapper.updateIsRelease(id, isRelease, new Date());
        }
    }

    public String getContentById(Long id) {
        return contentMapper.selectByPrimaryKey(id).getContent();
    }

    //收藏按钮，根据日报的id修改状态state为收藏/未收藏,并新增或删除收藏
    public void updateStateById(Long id, Integer state, Long userId) {
        //根据id设置状态为收藏/未收藏
        newsMapper.updateStateById(id, state);
        //根据id查询日报
        News news = newsMapper.selectByPrimaryKey(id);
        //判断是收藏操作还是取消收藏操作
        if (state == 1){
            Collect collect = new Collect();
            collect.setUserId(userId);
            collect.setType(1);
            collect.setCollectId(news.getId());
            collect.setTitle(news.getTitle());
            collect.setCoverUrl(news.getCoverUrl());
            collect.setCollectTime(new Date());
            collectMapper.insert(collect);
        } else {
            Collect collect = new Collect();
            collect.setUserId(userId);
            collect.setType(1);
            collect.setCollectId(news.getId());
            collectMapper.deleteByPrimaryKey(collect);
        }
    }

    public List<News> getNewsByTypeId(Long typeId, Long id) {

        return newsMapper.getNewsByTypeId(typeId, id);
    }

    public PageInfo<News> queryNewsStateByuserId(NewsQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<News> list = newsMapper.selectNewsStateByuserId(qo);
        return new PageInfo<>(list);
    }
}
